package licode.unisop.authcenter.adapter.web;

import licode.unisop.authcenter.application.executor.CenterAssetExecutor;
import licode.unisop.authcenter.application.info.*;
import licode.unisop.client.inter.HasAuthority;
import licode.unisop.types.AuthConst;
import licode.unisop.unilite.result.UniReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 定义统一认证平台中心领域相关接口
 *
 * @author licode
 */
@Slf4j
@UniReturnResult
@RestController
@RequestMapping("/uni-center/v1/asset")
public class CenterAssetController extends CenterBaseController {
    @Resource
    private CenterAssetExecutor centerAssetExecutor;

    /**
     * 保存资源信息
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void save(@RequestBody PlatAssetSaveIn asset) {
        centerAssetExecutor.save(getAuthIn(), asset);
    }

    /**
     * 把资源移动到目标资源下（改变其父节点）
     */
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void move(@RequestBody PlatAssetMoveIn req) {
        centerAssetExecutor.move(getAuthIn(), req);
    }

    /**
     * 删除资源
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void delete(@RequestBody PlatAssetDeleteIn req) {
        centerAssetExecutor.delete(getAuthIn(), req);
    }

    /**
     * 保存资源持有信息（比如保存分组下的权限信息）
     */
    @RequestMapping(value = "/relation/of-holder/save", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void saveAssetsOfHolder(@RequestBody SiSaveHoldAssetsIn req) {
        centerAssetExecutor.saveAssetsOfHolder(getAuthIn(), req);
    }

    /**
     * 移除持有的资源（比如移除分组下的权限信息）
     */
    @RequestMapping(value = "/relation/of-holder/remove", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void removeAssetsOfHolder(@RequestBody SiSaveHoldAssetsIn req) {
        centerAssetExecutor.removeAssetsOfHolder(getAuthIn(), req);
    }

    /**
     * 保存资源持有信息（比如保存持有权限的所有资源信息（比如分组、角色、用户等））
     */
    @RequestMapping(value = "/relation/to-holder/save", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void saveHoldersOfAsset(@RequestBody SiSaveAssetHoldsIn req) {
        centerAssetExecutor.saveHoldersOfAsset(getAuthIn(), req);
    }

    /**
     * 保存资源属性
     */
    @RequestMapping(value = "/attribute/save", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void saveAttributesOfAsset(@RequestBody SaveAttributesOfAssetIn req) {
        centerAssetExecutor.saveAttributesOfAsset(getAuthIn(), req);
    }

    /**
     * 移除资源属性
     */
    @RequestMapping(value = "/attribute/remove", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:update")
    public void removeAttributesOfAsset(@RequestBody RemoveAttributesOfAssetIn req) {
        centerAssetExecutor.removeAttributesOfAsset(getAuthIn(), req);
    }
}
